import { native } from 'qvog-dsl';
import { IfStmt, InvokeExpr, Value, InvokeStmt, FieldReference } from 'qvog-types';
import { QHint } from '~/adapter/Defines';

export default native<QHint>()()
    .match((node: Value): node is Value => {
        return node.stream().any(s => s instanceof FieldReference && s.name === "url")
    })
    .when((node, { native }) => {
        const sinkNodes = native.api.nodes(s => s instanceof InvokeStmt &&
            (expr => expr instanceof InvokeExpr && expr.target === "redirect")(s.expression), "Sink")
        const barrierNodes = native.api.nodes(s => s instanceof IfStmt, "Barrier")
        return native.api.exists(node, barrierNodes, sinkNodes, "Flow");

    })
    .report({table: 'Flow'});
